package online.mjchow.fic.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.InputStream;
import java.util.Properties;

/**
 * Created by mjchow on 16-10-30.
 */
public class JedisApi {

    private static String host = "";
    private static int port;
    private static String password = "";
    private static int maxTotal;
    private static int maxIdle;
    private static int maxWaitMillis;
    private static int timeout;

    private static JedisPool pool = null;

    //过期时间30天
    private static int expire = 60 * 60 * 24 * 30;

    static {
        InputStream inputStream = JedisApi.class.getResourceAsStream("/redis.properties");
        Properties redisProperties = new Properties();
        try {
            redisProperties.load(inputStream);
            inputStream.close();
        } catch (Exception e) {e.printStackTrace();}
        host = redisProperties.getProperty("redis.host");
        port = Integer.parseInt(redisProperties.getProperty("redis.port"));
        password = redisProperties.getProperty("redis.password");
        maxTotal = Integer.parseInt(redisProperties.getProperty("redis.maxTotal"));
        maxIdle = Integer.parseInt(redisProperties.getProperty("redis.maxIdle"));
        maxWaitMillis = Integer.parseInt(redisProperties.getProperty("redis.maxWaitMillis"));
        timeout = Integer.parseInt(redisProperties.getProperty("redis.timeout"));
    }

    private static JedisPool getPool() {
        if(pool==null) {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(maxTotal);
            poolConfig.setMaxIdle(maxIdle);
            poolConfig.setMaxWaitMillis(maxWaitMillis);
            pool = new JedisPool(poolConfig, host, port, timeout);
        }
        return pool;
    }

    public static Jedis getJedis() {
        Jedis jedis = null;
        if(pool==null) {
            pool = getPool();
        }
        jedis = pool.getResource();
        jedis.select(2);
        return jedis;
    }

    @Deprecated
    public static void put(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.set(key, value);
            jedis.expire(key, expire);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    /**
     *
     * @param key
     * @param value
     * @param expire
     */
    public static void put(String key, String value, int expire) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.set(key, value);
            jedis.expire(key, expire);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    public static String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * @param key
     * @param field
     * @param value
     */
    @Deprecated
    public static void hput(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hset(key, field, value);
//            jedis.expire(key, expire);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    /**
     *
     * @param key
     * @param field
     * @param value
     * @param expire
     */
    public static void hput(String key, String field, String value, int expire) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hset(key, field, value);
            jedis.expire(key, expire);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    public static String hget(String key, String field) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            value = jedis.hget(key, field);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return value;
    }

    public static void expire(String key, int expire) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.expire(key, expire);
        } finally {
            jedis.close();
        }
    }


    private static void testCloseResource() {
//      如果put操作对redis没有close的话，这里只能打印出5个，因为pool中的redis资源一直被占用着。其他操作也是一样的
        for(int i=0; i<10; i++) {
            put("test-"+i, "test-"+i);
            System.out.println("the - " + i);
        }
    }

    public static void main(String[] args) {
        for(int i=10; i<20; i++) {
            put("test-"+i, "test-"+i);
            System.out.println("the - " + i);
        }
    }
}
